package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.constant.Constant;
import com.csun.cmny.provider.entity.JsonResult;
import com.csun.cmny.provider.model.domain.OmcOldManAccountFunds;
import com.csun.cmny.provider.model.dto.RechargeQueryDto;
import com.csun.cmny.provider.pojo.*;
import com.csun.cmny.provider.pojo.Dictionary;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.vo.SettleDetailVo;
import com.csun.cmny.util.RedisUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 费用结算管理
 */
@RestController
@RequestMapping("/feeSettle")
public class FeeSettleController extends BaseController {
    @Autowired
    private OldmanCheckInService oldmanCheckInService;
    @Autowired
    private OldmanNurseService oldmanNurseService;
    @Autowired
    private OldmanFeeService oldmanFeeService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private OldmanService oldmanService;
    @Autowired
    private OldmanRechargeService oldmanRechargeService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 查询费用结算列表
     */
    @RequestMapping(value = "/showAll", method = RequestMethod.POST)
    public Wrapper showAll(@RequestBody Map<String,Object> queryMap){
        String pageNo = (String) queryMap.get("pageNo");
        String pageSize = (String) queryMap.get("pageSize");
        queryMap.put("homeId",getLoginAuthDto().getGroupId());
        if (StringUtils.isEmpty(pageNo)) {
            pageNo = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        List<Map<String,Object>> mapList = oldmanFeeService.showAll(queryMap);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(mapList);
        /*if (CollectionUtils.isEmpty(mapList)) {
            return new JsonResult(false, "没有查询到结果", new ArrayList<>());
        }*/
//        return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 查询费用结算列表
     * @param: [rechargeQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody RechargeQueryDto rechargeQueryDto){
        PageHelper.startPage(rechargeQueryDto.getPageNum(),rechargeQueryDto.getPageSize());
        rechargeQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<SettleDetailVo> voList = oldmanFeeService.queryListWithPage(rechargeQueryDto);
        PageInfo<SettleDetailVo> pageInfo = new PageInfo<>(voList);
//        return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 充值页面回显
     */
    @RequestMapping(value="rechargeReviw",method = RequestMethod.POST)
    public Wrapper rechargeReviw(HttpServletRequest req, Integer oldmanId){
        String uuid=UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set("randomuuid", uuid);
        //校验
        if (oldmanId == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"老人id不能为空",oldmanId);
        }
        OldmanFee oldmanFee =  oldmanFeeService.selectByOldmanId(oldmanId);
//        if (oldmanFee==null){
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该老人暂无消费记录",oldmanId);
//        }
        Oldman oldman = new Oldman();
        oldman.setOldmanId(oldmanId);
        Oldman oldman1= oldmanService.selectByOld(oldman);
        List<Dictionary> dictionarieList = dictionaryService.selectByType(Constant.FUKUAN);
        OldmanRechargeVo vo = new OldmanRechargeVo();
        vo.setOldmanId(oldmanId);
        vo.setOldmanName(oldman1.getOldName());
        vo.setDictionaryList(dictionarieList);
        //当前登录者
        LoginAuthDto loginAuthDto = getLoginAuthDto();

        vo.setOperatorId(loginAuthDto.getUserId().intValue());
        vo.setOperatorName("管理员");
        vo.setRechargeDate(DateUtil.getCurrentDate());
        vo.setUuid(uuid);
//        return  WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), vo);
    }

    /**
     * 充值
     */
    @RequestMapping(value="/recharge",method = RequestMethod.POST)
    public Wrapper recharge(HttpServletRequest request,@RequestBody OldmanRecharge oldmanRecharge){
        if (oldmanRecharge.getOldmanId() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"老人id不能为空",null);
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"oldman.id.null",null);
        }
        // 校验老人是否在院
        Oldman oldmanQuery = new Oldman();
        oldmanQuery.setOldmanId(oldmanRecharge.getOldmanId());
        oldmanQuery = oldmanService.selectByOld(oldmanQuery);
        if (!oldmanQuery.getStatus().equals(Constant.YIRUYUAN)) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"老人不是入院状态",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"oldman.not.in",null);
        }
        if (oldmanRecharge.getOldmanName() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"老人名称不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"advisoryCancel.oldName.null",null);
        }
        if (oldmanRecharge.getRechargeDate() == null) {
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"充值日期不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"date.is.null",null);
        }
        if (oldmanRecharge.getOperatorId() == null) {
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"操作人id不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"operator.id.null",null);
        }
        if (oldmanRecharge.getOperatorName() == null) {
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"操作人名称不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"operator.name.null",null);
        }
        if (oldmanRecharge.getDictionaryId() == null) {
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"充值方式不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"recharge.way.null",null);
        }
        if (oldmanRecharge.getRechargeMoney() == null) {
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"充值金额不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"recharge.number.null",null);
        }
        if (oldmanRecharge.getUuid() == null) {
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"随机数不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"uuid.is.null",null);
        }
        String uuid = (String) redisTemplate.opsForValue().get("randomuuid");
        //充值
        int ret = oldmanRechargeService.recharge(oldmanRecharge,uuid);
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 老人充值记录
     */
    @RequestMapping(value="showRechargeAll",method = RequestMethod.POST)
    public Wrapper showRechargeAll(@RequestBody Map<String,Object> queryMap){
        String pageNo = (String) queryMap.get("pageNo");
        String pageSize = (String) queryMap.get("pageSize");
        String oldmanId = (String) queryMap.get("oldmanId");

        if (StringUtils.isEmpty(pageNo)) {
            pageNo = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }
        if (oldmanId == null) {
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"老人id不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"oldman.id.null",null);
        }
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        List<OldmanRecharge> oldmanRechargeList = oldmanRechargeService.showRechargeAll(queryMap);
        PageInfo<OldmanRecharge> pageInfo = new PageInfo<>(oldmanRechargeList);
//        return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }
}
